<?php
namespace Home\Controller;
use Think\Controller;
class UserInfoController extends BaseController {
    //userTop.html页面
    public function userTop(){
        $this->display();
    }
    
    //navLeftOne页面
    public function navLeftOne(){
        $this->display();
    }
    
    //navLeftTwo页面
    public function navLeftTwo(){
        $this->display();
    }
    
    //载入用户中心页面
    public function index(){
        $infolist = M('info'); 
        $user_id = session('id');
        /***********实现分页效果************/
        //1 总记录条数
        $cnt = $infolist->where("user_id='$user_id'")->count();     //获得总记录条数
        $pag = 3;   //没页显示7条

        //2 分页类对象
        $page = new \Tools\Page($cnt,$pag);

        //3 获取每页的数据信息
        $info = $infolist
            ->limit($page->offset,$pag)
            ->alias('i')
            ->join('tp_category c on i.cat_id=c.cat_id')
            ->group('info_id')
            ->where(array('i.user_id'=>$user_id))
            ->order('info_overtime')
            ->select();
        $this->assign('info',$info);
        $this->display();
    }
    
    //载入信息管理页面
    public function infoList(){
        $infolist = M('info'); 
        $user_id = session('id');
        /***********实现分页效果************/
        //1 总记录条数
        $cnt = $infolist->where("user_id='$user_id'")->count();     //获得总记录条数
        $pag = 5;   //没页显示5条

        //2 分页类对象
        $page = new \Tools\Page($cnt,$pag);

        //3 获取每页的数据信息
        $info = $infolist
            ->limit($page->offset,$pag)
            ->alias('i')
            ->join('tp_category c on i.cat_id=c.cat_id')
            ->group('info_id')
            ->where(array('i.user_id'=>$user_id))
            ->order('info_overtime')
            ->select();
        $this->assign('info',$info);
        //4制作页码列表
        $pagelist = $page->fpage(array(0,3,4,6,7));//只显示指定下标内容
        $this->assign('pagelist',$pagelist);
        $this->display();
    }

    //更新发布时间
    public function upDateTime(){
        //更新时间
        $time = time();
        $value = $_GET['id']+0;
        $upinfotime = M('info');
        $upinfotime->info_overtime = $time+10*24*3600;
        $z=$upinfotime->where("info_id= '$value' ")->save();
        
        if($z){
            $this->redirect('infoList',array(),2,'更新成功!');
        }else{
            $this->redirect('infoList',array(),2,'更新失败!');
        }
    }
    
    //载入个人资料页面
    public function user(){
        
        $user_id = session('id');
        $userinfo = new \Model\UserinfoModel();
        if(IS_POST){
            //接收数据  
            //$data['user_name'] = $_POST['user_name'];
            $data['user_linkman'] = $_POST['user_linkman'];
            $data['user_addr'] = $_POST['user_addr'];
            $data['real_name'] = $_POST['real_name'];
            $data['user_sex'] = $_POST['user_sex'];
            $data['id_number'] = $_POST['id_number'];
            $data['user_tel'] = $_POST['user_tel'];
            $data['user_qq'] = $_POST['user_qq'];
            $z = $userinfo->where("user_id='$user_id'")->save($data); 
            //2 AR形式$model -> 属性(字段) = 值;
            if($z){
                $this->redirect('user',array(),2,'信息修改成功!');
            }else{
                $this->redirect('user',array('goods_id'=>$goods_id),2,'您没有任何修改信息!');
            }
        }else{
            $info = $userinfo->where("user_id='$user_id'")->select();
            $this->assign('info',$info);
            $this->display();
        }

    }
    
    //载入消息留言页面
    public function liuyan(){
        $this->display();
    }
    
    //载入我的抽奖页面
    public function lottery(){
        $this->display();
    }
    
    //载入修改密码页面
    public function changePassWord(){

        if(IS_POST){

            //接收数据
            $oldPassWord = $_POST['oldPassWord'];
            $newPassWord = $_POST['newPassWord'];
            $newPassWord1 = $_POST['newPassWord1'];
            $len = strlen($newPassWord1);
            $user_id = session('id');
            $data = M('user');
            
            $pwd = $data->where("user_id='$user_id' and pwd='$oldPassWord'")->find();
            if($pwd){ //判断该id对应的用户的密码与接收的旧密码是否相同

                if($newPassWord===$newPassWord1){//判断要修改的新密码两次是否输入相同

                    if($len < 6){

                        $this->error("亲,密码长度不能小于6位哦!");

                    }else{

                        if($oldPassWord===$newPassWord1){

                            $this->error("新密码与旧密码相同啦!不需要更改");

                        }else{

                            //销毁session 并跳转到登录页面
                            unset($_SESSION['name']);
                            session_destroy();
                            $data->where("user_id= $user_id ")->setField('pwd',$newPassWord1);
                            $this->success('密码修改成功,请重新登录!','../Login/index');

                        }

                    }
                    
                }else{

                    $this->error("亲,细心点哦!两次输入的密码不一致");

                }

            }else{

                $this->error("旧密码输入错误!!");

            }

        }else{

            $this->display();
        }
    }

    

    //载入商家管理页面
    public function company(){
        $this->display();
    }
    
    //载入微信公众管理页面
    public function weixin(){
        $this->display();
    }







}